package com.ruding.disruptor.utils;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

	public static List<File> getFileList(String strPath, String filter) {
        List<File> filelist = new ArrayList<>();
        File dir = new File(strPath);
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                    filelist.addAll(getFileList(files[i].getAbsolutePath(), filter)); // 获取文件绝对路径
                } else if (fileName.endsWith(filter)) { // 判断文件名是否以.avi结尾
                	System.out.println(files[i].getAbsolutePath());
                    filelist.add(files[i]);
                } else {
                    continue;
                }
            }
        }
        return filelist;
    }
	
	public static List<File> getFileDirectoryList(String strPath) {
        List<File> filelist = new ArrayList<>();
        File dir = new File(strPath);
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                	if(files[i].listFiles().length==0){
                		continue;
                	}
                	File[] subFiles = files[i].listFiles();
                	if (subFiles != null) {
                        for (int j = 0; j < subFiles.length; j++) {
                        	if(subFiles[j].isFile()){
                        		filelist.add(files[i]);
                        		break;
                        	}
                        }
                	}
                	filelist.addAll(getFileDirectoryList(files[i].getAbsolutePath())); // 获取文件绝对路径
                } else {
                    continue;
                }
            }

        }
        return filelist;
    }
	
	public static Long getFileCreateTime(String filePath){
	    File file = new File(filePath);
	    try {
	        Path path= Paths.get(filePath);
	        BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
	        BasicFileAttributes attr = basicview.readAttributes();
	        return attr.creationTime().toMillis();
	    } catch (Exception e) {
	        e.printStackTrace();
	        return file.lastModified();
	    }
	}
	
	public static Long getFileMaxTime(File file){
		long create,update=file.lastModified();
	    try {
	        Path path= Paths.get(file.getAbsolutePath());
	        BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
	        BasicFileAttributes attr = basicview.readAttributes();
	        create = attr.creationTime().toMillis();
	    } catch (Exception e) {
	        e.printStackTrace();
	        create = update;
	    }
	    if(create>update){
	    	return create;
	    }else{
	    	return update;
	    }
	}
	
	public static void main(String[] args) {
		System.out.println(getFileList("D:\\",".pdf").size());
	}
	
}
